﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FyndSharp.Data;
using Fynd.Hermes.Web.Objects;
using FyndSharp.Data.Orm;
using System.Data.Common;
using System.Data;
using FyndSharp.Utilities.Common;
using FyndSharp.BusinessLogic;

namespace Fynd.Hermes.Web.Pages
{
    public partial class UserEdit : RoleBasedPage
    {
        protected UserInfomation EditingUser = new UserInfomation();
        protected UserInfomation CurrentUser = null;
        protected UserLogic CurrentUserLogic = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            CurrentUser = Context.Items[Global.HttpContextKeyUser] as UserInfomation;
            CurrentUserLogic = new DefaultLogicFactory().CreateLogic<UserLogic>(new DefaultLogicUser(CurrentUser));

            LoadResultBy(TypeConvert.ToString(Request.QueryString["id"]));

            if (!IsPostBack)
            {
                if (!String.IsNullOrEmpty(EditingUser.Id))
                {
                    TextBoxEmployeeNumber.Enabled = false;
                    TextBoxLoginName.Enabled = false;
                    TextBoxTrueName.Enabled = false;
                }
                TextBoxEmployeeNumber.Text = EditingUser.EmployeeNumber;
                TextBoxLoginName.Text = EditingUser.LoginName;
                TextBoxTrueName.Text = EditingUser.TrueName;
                DropDownListRoles.SelectedValue = EditingUser.RoleName;
            }
        }

        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            bool isPasswordChanged = false;
            if (String.IsNullOrEmpty(EditingUser.Id))
            {
                isPasswordChanged = true;

                if (String.IsNullOrEmpty(this.TextBoxLoginName.Text))
                {
                    LabelMessage.Text = "登录帐号都不输入啊？";
                    return;
                }
                EditingUser.LoginName = this.TextBoxLoginName.Text;
                if (CurrentUserLogic.CheckLoginNameExists(EditingUser.LoginName))
                {
                    LabelMessage.Text = "已经有其他人用了这个登录帐号了哦。";
                    return;
                }


                if (String.IsNullOrEmpty(this.TextBoxEmployeeNumber.Text))
                {
                    LabelMessage.Text = "工号多少？";
                    return;
                }
                EditingUser.EmployeeNumber = this.TextBoxEmployeeNumber.Text;

                if (String.IsNullOrEmpty(this.TextBoxTrueName.Text))
                {
                    LabelMessage.Text = "叫什么名字还是要搞清楚滴？";
                    return;
                }
                EditingUser.TrueName = this.TextBoxTrueName.Text;
            }

            if (!String.IsNullOrEmpty(this.TextBoxPassword.Text))
            {
                isPasswordChanged = true;
            }
            if (isPasswordChanged)
            {
                if (String.IsNullOrEmpty(this.TextBoxPassword.Text))
                {
                    LabelMessage.Text = "请输入密码～";
                    return;
                }

                if (this.TextBoxPassword.Text.Length < 6)
                {
                    LabelMessage.Text = "密码太短啦～";
                    return;
                }
                if (this.TextBoxPassword.Text.Equals(this.TextBoxPassword2.Text, StringComparison.CurrentCulture) == false)
                {
                    LabelMessage.Text = "两次密码输入不一样哦～";
                    return;
                }
                EditingUser.Password = this.TextBoxPassword.Text;
            }
            
            try
            {
                UserRole role = (UserRole)Enum.Parse(typeof(UserRole), this.DropDownListRoles.SelectedValue);
                EditingUser.RoleName = role.ToString();
            }
            catch
            {
                LabelMessage.Text = "No, no, no, 乱给角色可不行。";
                return;
            }

            try
            {
                Save();
                Response.Redirect("UserList.aspx");
            }
            catch
            {
                LabelMessage.Text = "保持用户数据时发生了点其他异常，杯具啊！";
                return;
            }
        }

        private void Save()
        {

            CurrentUserLogic.SaveUser(EditingUser);
        }

        private void LoadResultBy(string anUserId)
        {
            AdoSession db = AdoSessionManager.Current["hermes"];
            bool isDbOpen = false;
            try
            {
                isDbOpen = db.Open();

                string sql = OrmObjectFactory.CreateSelectSql(typeof(UserInfomation));
                sql += " WHERE user_id = @id";
                using (DbCommand cmd = db.CreateCommand(sql))
                {
                    cmd.Parameters.Add(db.CreateParameter("@id", DbType.String, anUserId));

                    DataTable dt = db.ExecuteDataTable(cmd);
                    if (dt.Rows.Count > 0)
                    {
                        EditingUser = OrmObjectFactory.CreateOrmObject<UserInfomation>(dt.Rows[0]);
                    }
                }
            }
            finally
            {
                if (isDbOpen)
                {
                    db.Close();
                }
            }
        }

        protected override IEnumerable<UserRole> AccreditedRoles
        {
            get
            {
                return new UserRole[] { UserRole.Administrator };
            }
        }
    }
}